/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.model.highscore;

import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tommy
 */
public class HighScoreXML {
    public static void saveHighScores(List<HighScore> highscores, String filename) {
        Collections.sort(highscores);
        Collections.reverse(highscores);
        
        try {
            OutputStream os = new FileOutputStream(new File("assets/Interface/" + filename));
            XStream xs = new XStream();
            xs.processAnnotations(HighScore.class);
            xs.toXML(highscores,os);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(HighScoreXML.class.getName()).log(Level.SEVERE, "Could not save highscores", ex);
        }
    }
    
    public static List<HighScore> loadHighScores(String filename) {
        
        try {
            InputStream is = new FileInputStream(new File("assets/Interface/" + filename));
            XStream xs = new XStream();
            xs.processAnnotations(HighScore.class);
            List<HighScore> highscores = ((List<HighScore>)xs.fromXML(is));
            Collections.sort(highscores);
            Collections.reverse(highscores);
            return highscores;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(HighScoreXML.class.getName()).log(Level.SEVERE, "File highscores.xml not found", ex);
            return new LinkedList<HighScore> ();
        }
    }
    
}
